package com.example.demo.dao.impl;

import com.example.demo.dao.BaseDaoImpl;
import com.example.demo.dao.IVisaCardDao;
import com.example.demo.model.po.VisaPage;
import com.example.demo.model.request.VisaPageRequest;
import com.example.demo.model.request.VpsPageRequest;
import com.example.demo.util.AssertUtils;
import com.example.demo.util.DateUtils;
import org.jooq.Condition;
import org.jooq.Record;
import org.jooq.SelectJoinStep;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

import static com.example.demo.jooq.tables.VisaCard.VISA_CARD;
import static com.example.demo.jooq.tables.Vps.VPS;
import static org.jooq.impl.DSL.count;

@Component
public class IVisaCardDaoImpl extends BaseDaoImpl implements IVisaCardDao {


    @Override
    public List<VisaPage> queryPageVisaList(VisaPageRequest visaPageRequest) {
        //查询条件
        List<Condition> conditions = new ArrayList<>();
        if(AssertUtils.isNotEmpty(visaPageRequest.getKeyword())){
            conditions.add(VISA_CARD.VISA_CARD_NO.likeRegex(visaPageRequest.getKeyword().trim()));
        }
        if(AssertUtils.isNotEmpty(visaPageRequest.getCardStatus())){
            conditions.add(VISA_CARD.CARD_STATUS.likeRegex(visaPageRequest.getCardStatus()));
        }
        if(AssertUtils.isNotEmpty(visaPageRequest.getCreatedDate())){
            conditions.add(VISA_CARD.CREATED_DATE.eq(DateUtils.stringToTime(visaPageRequest.getCreatedDate())));
        }
        if(AssertUtils.isNotEmpty(visaPageRequest.getVisaChannel())){
            conditions.add(VISA_CARD.VISA_CHANNEL.eq(visaPageRequest.getVisaChannel()));
        }
        if(AssertUtils.isNotEmpty(visaPageRequest.getVisaType())){
            conditions.add(VISA_CARD.VISA_TYPE.eq(visaPageRequest.getVisaType()));
        }
        SelectJoinStep<Record> totalLine = this.getDslContext()
                .select(VISA_CARD.fields())
                .select(count().over().as("totalLine"))
                .from(VISA_CARD);
        totalLine.where(conditions);
        return totalLine.orderBy(VISA_CARD.CREATED_DATE.desc())
                .offset(visaPageRequest.getOffset())
                .limit(visaPageRequest.getPageSize())
                .fetchInto(VisaPage.class);
    }
}
